package com.sc.nft.dao;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.CollectiveOrder;
import com.sc.nft.entity.dto.RefundOrderPageDTO;
import com.sc.nft.entity.vo.RefundOrderPageVO;
import com.sc.nft.enums.OrderStatusEnum;
import com.sc.nft.mapper.CollectiveOrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 集售订单表(sc_collective_order)数据DAO
 *
 * @author zxy
 * @since 2024-09-18 01:01:58
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class CollectiveOrderDao extends ServiceImpl<CollectiveOrderMapper, CollectiveOrder> {

    public Integer getUserWaitPayOrderCount(Long userId){
        return lambdaQuery().eq(CollectiveOrder::getBuyUserId, userId).eq(CollectiveOrder::getStatus,OrderStatusEnum.WAIT_PAY).count();
    }

    public Page<CollectiveOrder> getUserWaitPayOrderPage(Page page, Long userId){
        return lambdaQuery().gt(CollectiveOrder::getCloseTime,DateTime.now()).eq(CollectiveOrder::getBuyUserId, userId).eq(CollectiveOrder::getStatus,OrderStatusEnum.WAIT_PAY).orderByDesc(CollectiveOrder::getId).page(page);
    }

    public List<CollectiveOrder> getUserWaitPayOrderBuyIds(Long userId,List<Long> ids){
        return lambdaQuery().gt(CollectiveOrder::getCloseTime,DateTime.now()).eq(CollectiveOrder::getBuyUserId, userId).eq(CollectiveOrder::getStatus,OrderStatusEnum.WAIT_PAY).in(CollectiveOrder::getId,ids).list();
    }
    public List<CollectiveOrder> listByIds(List<Long> ids){
        return lambdaQuery().in(CollectiveOrder::getId,ids).list();
    }
    public boolean updateStatusToClose(Long id){
        return lambdaUpdate().eq(CollectiveOrder::getId,id).eq(CollectiveOrder::getStatus,OrderStatusEnum.WAIT_PAY).set(CollectiveOrder::getCloseTime,DateTime.now()).set(CollectiveOrder::getStatus,OrderStatusEnum.CLOSE).update();
    }

    public List<CollectiveOrder> getPreTimeOutOrderIds(DateTime offset) {
        return lambdaQuery().eq(CollectiveOrder::getStatus, OrderStatusEnum.WAIT_PAY).le(CollectiveOrder::getCloseTime,offset).list();
    }


    public List<CollectiveOrder> getWaitSettleOrder() {
        return lambdaQuery().eq(CollectiveOrder::getStatus, OrderStatusEnum.PAYMENT).eq(CollectiveOrder::getSettleFlag,false).orderByAsc(CollectiveOrder::getId) .list();
    }

    public boolean updateSettleFlag(Long id){
        return lambdaUpdate().eq(CollectiveOrder::getId,id).eq(CollectiveOrder::getSettleFlag,false).set(CollectiveOrder::getSettleFlag,true).update();
    }

    public CollectiveOrder getPaySuccessBySalesId(Long salesId){
        return lambdaQuery().eq(CollectiveOrder::getStatus, OrderStatusEnum.PAYMENT).eq(CollectiveOrder::getCollectiveSalesId,salesId).one();
    }

    public IPage<RefundOrderPageVO> refundOrderPage(RefundOrderPageDTO request, Long userId) {
        QueryWrapper<CollectiveOrder> wrapper = Wrappers.query();
        wrapper.select("order_no,collection_name,buy_price,status,create_time,pay_time");
        wrapper.eq("buy_user_id",userId);
        wrapper.eq(request.getStatus()!=null && request.getStatus()!=OrderStatusEnum.ALL,"status",request.getStatus());
        wrapper.eq(StrUtil.isNotEmpty(request.getOrderNo()),"order_no",request.getOrderNo());
        wrapper.orderByDesc("id");
        Page page = page(request.toMp(), wrapper);
        List<CollectiveOrder> records = page.getRecords();
        List<RefundOrderPageVO> refundOrderPageVOS = new ArrayList<>();
        records.forEach(collectiveOrder -> {
            RefundOrderPageVO refundOrderPageVO=new RefundOrderPageVO();
            refundOrderPageVO.setOrderNo(collectiveOrder.getOrderNo());
            refundOrderPageVO.setFirstTitle(collectiveOrder.getCollectionName());
            refundOrderPageVO.setBuyPrice(collectiveOrder.getBuyPrice());
            refundOrderPageVO.setStatus(collectiveOrder.getStatus());
            refundOrderPageVO.setPayTime(collectiveOrder.getPayTime());
            refundOrderPageVO.setCreateTime(collectiveOrder.getCreateTime());
            refundOrderPageVOS.add(refundOrderPageVO);
        });
        page.setRecords(refundOrderPageVOS);
        return page;
    }
}